<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <!-- <script>
        // 运算符
        // 逻辑与（&&）短路
        const name = "";
        const message = name && `Hello, ${name}`;
        console.log(message); // 输出：""（空字符串）

        // 逻辑或（||）短路
        const userRole = null;
        const role = userRole || "Guest";
        console.log(role); // 输出："Guest"
    </script> -->

    <!-- <script>
        const user = {
            name: "Alice",
            address: {
                street: "123 Main St"
            }
        };

        // 安全访问可能不存在的属性
        const street = user.address?.street;
        console.log(street); // 输出："123 Main St"

        const zipCode = user.address?.zipCode;
        console.log(zipCode); // 输出：undefined（不会报错）

        // 安全调用可能不存在的方法
        const length = user.name?.toUpperCase()?.length;
        console.log(length); // 输出：5

        const greet = user.sayHello?.();
        console.log(greet); // 输出：undefined
    </script> -->

    <script>
        const username = undefined;
        const displayName = username ?? "Anonymous";
        console.log(displayName); // 输出："Anonymous"

        const count = 0;
        const showCount = count ?? "No count";
        console.log(showCount); // 输出：0（0 不被视为空值）

        const isActive = false;
        const status = isActive ?? "Inactive";
        console.log(status); // 输出：false（false 不被视为空值）
    </script>
</body>

</html>